package com.va.feedback.component;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.va.feedback.FeedbackItemConstants;

public class TextFeedback extends AbstractFeedback {

	private EditText textWidget;
	private Context context;

	/**
	 * Constructor for TextFeedback
	 * @param context context of the parent
	 */
	public TextFeedback(Context context) {
		super(context);
		this.context = context;
		
		addQuestionWidget();
		addAnswerComponent();
	}
	
	/**
	 * Sets the properties
	 */
	public void setComponentProperty(String type, Object value) {
		if (FeedbackItemConstants.PROPERTY_QUESTION.equals(type)) {
			String question = (String) value;
			questionWidget.setText(question);
		} else if (FeedbackItemConstants.PROPERTY_TEXT_HINT.equals(type)) {
			String textHint = (String) value;
			textWidget.setHint(textHint);
		}
	}
	
	/**
	 * Adds the component which captures the user response to the question widget
	 */
	protected void addAnswerComponent() {
		textWidget = new EditText(context);
		RelativeLayout.LayoutParams textWidgetLayout = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		textWidget.setId(2);
		textWidgetLayout.addRule(RelativeLayout.BELOW, 1);
		this.addView(textWidget, textWidgetLayout);
	}

	/**
	 * Returns the response in the form of a JSONObject
	 * The response is : {'qid': qid, 'answer': text answer}
	 */
	public JSONObject getFeedback() throws JSONException {
		String answer = textWidget.getText().toString().trim();
		if (answer.equals(""))
			return null;
		JSONObject object = new JSONObject();
		object.put("qid", qid);
		object.put("answer", answer);
		return object;
	}

}
